//
//  GJFEditSignViewController.m
//  Wedding
//
//  Created by 郭静芬 on 2017/1/12.
//  Copyright © 2017年 郭静芬. All rights reserved.
//

#import "GJFEditSignBackViewController.h"
#import "IQTextView.h"
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@interface GJFEditSignBackViewController () <MFMailComposeViewControllerDelegate>
@property (nonatomic, strong) IQTextView *editSign;
@end

@implementation GJFEditSignBackViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self.editSign becomeFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.96 alpha:1.00];
    if (self.editPersonMineStyle == EditPersonStyleSign) {
        self.editSign.placeholder = @"请您输入个性签名";
        self.navigationItem.title = @"个性签名";
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(saveSign)];
    }
    else {
        self.editSign.placeholder = @"请您输入用户反馈";
        self.navigationItem.title = @"用户反馈";
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(send)];
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(popVC)];
//    [self editSign];
//    [self send];
}
- (void)popVC {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)saveSign {
    if (self.editSign.text.length == 0) {
        [self.view showWarning:@"请输入个性签名"];
        return;
    }
    
    [self.view showBusyHUD];
    NSDictionary *dic = @{@"signature": self.editSign.text};
    [GJFNetworkTool POSTRequestWithUrl:APP_MINE_SIGN params:dic complete:^(NSDictionary *result) {
        [self.view hideBusyHUD];
        if ([result[@"code"] intValue] == 1000) {
            GJFUserInfo *userInfo = [GJFSingleTool shareInstance].userInfo;
            userInfo.signature = self.editSign.text;
            [GJFSingleTool saveUserInfo];
            [self popVC];
        }
    }];
}

- (void)send {
    if (self.editSign.text.length == 0) {
        [self.view showWarning:@"请输入用户反馈"];
        return;
    }
    
    [self sendMessage];
//    [self.view showBusyHUD];
//    NSDictionary *dic = @{@"content": self.editSign.text, @"contact":[[[GJFSingleTool shareInstance] userInfo] mobile_number]?:@"111"};
//    [GJFNetworkTool POSTRequestWithUrl:APP_MINE_FEEDBACK params:dic complete:^(NSDictionary *result) {
//        [self.view hideBusyHUD];
//        if ([result[@"code"] intValue] == 1000) {
//            [self.view showWarning:@"发送成功"];
//            [self performSelector:@selector(popVC) withObject:self afterDelay:1];
//        }
//    }];
}

- (void)sendMessage {
    MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    NSArray *toRecipients = [NSArray arrayWithObject:@"guojingfen11@icloud.com"];
    [controller setToRecipients:toRecipients];
    [controller setSubject:@"意见反馈"];
    [controller setMessageBody:self.editSign.text isHTML:NO];
    
    [self presentViewController:controller animated:YES completion:^{
        
    }];
}

- (void)mailComposeController:(MFMailComposeViewController*)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError*)error;
{
    [self dismissViewControllerAnimated:YES completion:^{
        [self popVC];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.editSign resignFirstResponder];
}

- (IQTextView *)editSign {
    if (!_editSign) {
        _editSign = [[IQTextView alloc] initWithFrame:CGRectMake(0, 20 * YY, WIDTH, 80 * YY)];
        _editSign.font = [UIFont systemFontOfSize:16 * XX];
        [self.view addSubview:_editSign];
    }
    return _editSign;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
